//时间比较 13:00:00


export const timeCompare = (time1, time2) => {
  if (!time1) {
    return;
  }
  let time1arr = time1 && time1.split(':') || []
  let time2arr = time2 && time2.split(':') || []
  let time1addDate = new Date()
  let time2addDate = new Date()
  if (time1arr[0]) {
    time1addDate.setHours(time1arr[0])
  }
  if (time1arr[1]) {
    time1addDate.setMinutes(time1arr[1])
  }
  if (time1arr[2]) {
    time1addDate.setSeconds(time1arr[2])
  }
  if (time2arr[0]) {
    time2addDate.setHours(time2arr[0])
  }
  if (time2arr[1]) {
    time2addDate.setMinutes(time2arr[1])
  }
  if (time2arr[2]) {
    time2addDate.setSeconds(time2arr[2])
  }
  return time1addDate.getTime() - time2addDate.getTime()
}

//日期比较
export const dateCompare = (time1, time2) => {
  let time1Long = new Date(...getDateArr(time1))
  let time2Long;
  if (time2) {
    time2Long = new Date(...getDateArr(time2))
  } else {
    time2Long = new Date().getTime()
  }
  return time1Long - time2Long
}

//日期格式化
// 栗子
// 1. (YY)YY-MM-DD hh:mm:ss => (20)19-10-04 10:02:02
// 2. (YY)YY年MM月DD日 hh时mm分ss秒 => (20)19年10月04日 10时02分02秒
export const formatDate = (oDate, sFormation, add) => {
  let date
  if (!oDate) {
    date = new Date()
  } else if (/^\d+$/.test(oDate)) {
    date = new Date(oDate)
  } else {
    date = new Date(...getDateArr(oDate))
  }
  if (add) {
    date.setDate(date.getDate() + add)
  }
  let YYYY = date.getFullYear() + ''
  let YY = YYYY && YYYY.substring(2)
  let MM = date.getMonth() + 1
  let DD = date.getDate()
  let hh = date.getHours()
  let mm = date.getMinutes()
  let ss = date.getSeconds()
  let dateall = {
    YYYY: YYYY,
    YY: YY,
    MM: MM > 9 ? MM : '0' + MM,
    DD: DD > 9 ? DD : '0' + DD,
    hh: hh > 9 ? hh : '0' + hh,
    mm: mm > 9 ? mm : '0' + mm,
    ss: ss > 9 ? ss : '0' + ss
  }
  if (sFormation) {
    let timeStr = ''
    let reDate = /((YYYY)|(YY))?([^YMDhms]+)?(MM)?([^YMDhms]+)?(DD)?([^YMDhms]+)?(hh)?([^YMDhms]+)?(mm)?([^YMDhms]+)?(ss)?([^YMDhms]+)?/
    let year = ''
    let year_unit = ''
    let month = ''
    let month_unit = ''
    let day = ''
    let day_unit = ''
    let hour = ''
    let hour_unit = ''
    let min = ''
    let min_unit = ''
    let sec = ''
    let sec_unit = ''

    sFormation.replace(reDate, function(
      $0,
      $1,
      $2,
      $3,
      $4,
      $5,
      $6,
      $7,
      $8,
      $9,
      $10,
      $11,
      $12,
      $13,
      $14
    ) {
      year = ($1 && dateall[$1]) || ''
      year_unit = $4 || ''
      month = ($5 && dateall[$5]) || ''
      month_unit = $6 || ''
      day = ($7 && dateall[$7]) || ''
      day_unit = $8 || ''
      hour = ($9 && dateall[$9]) || ''
      hour_unit = $10 || ''
      min = ($11 && dateall[$11]) || ''
      min_unit = $12 || ''
      sec = ($13 && dateall[$13]) || ''
      sec_unit = $14 || ''
      return ''
    })

    timeStr = `${year}${year_unit}${month}${month_unit}${day}${day_unit}${hour}${hour_unit}${min}${min_unit}${sec}${sec_unit}`
    return timeStr
  } else {
    return dateall
  }
}

// 获取date对象
export const getOdate = oDate => {
  let date
  if (!oDate) {
    date = new Date()
  } else if (/^\d+$/.test(oDate)) {
    date = new Date(oDate)
  } else {
    date = new Date(...getDateArr(oDate))
  }
  return date
}

//获取时间戳
//
export const getTimeLong = (time) => {
  let re = /^\d+$/
  if (re.test(time)) {
    return time
  } else {
    let dateArr = getDateArr(time) || []
    let timeLong = new Date(...dateArr)
    return timeLong
  }
}

//获取当天一个时间节点的时间戳
export const getTimeWithTime = (time) => {
  let oDate = formatDate(null, 'YYYY-MM-DD')
  let dateArr = getDateArr(`${oDate} ${time}`) || []
  let timeLong = new Date(...dateArr)
  return timeLong.getTime()
}
//判断一个时间在不在指定的时间段内
export const judytimein = (time, { startTime, endTime }) => {
  let timeLong, startTimeLong, endTimeLong;
  let timePosition = ''
  if (!time) {
    timeLong = new Date().getTime()
  } else {
    let dateArr = getDateArr(`${time}`) || []
    timeLong = new Date(...dateArr)
  }
  let dateArrStartTime = getDateArr(`${startTime}`) || []
  let dateArrEndTime = getDateArr(`${endTime}`) || []
  startTimeLong = new Date(...dateArrStartTime)
  endTimeLong = new Date(...dateArrEndTime)
  if (timeLong >= startTimeLong && timeLong < endTimeLong) {
    timePosition = 'in'
  } else if (timeLong < startTimeLong) {
    timePosition = 'before'
  } else {
    timePosition = 'finish'
  }
  return timePosition
}

/**
 * 获取日期数组 2018-10-22 10:23:22 / 2018/10/22 10:23:22  [2018,10,22,10,23,22]
 * @param {*} datetime 
 */
const getDateArr = (datetime) => {
  if (!datetime) {
    return []
  }
  let timearr = datetime.split(' ')
  let date = timearr[0]
  let time = timearr[1]
  let returnArr = []
  let dateArr = []
  let timeArr = time && time.split(":") || []
  if (/-/g.test(date)) {//2018-1-1格式
    dateArr = date.split('-')
  } else if (/\//g.test(date)) {//2018/1/1格式
    dateArr = date.split('/')
  }
  dateArr[1] = dateArr[1] - 1
  returnArr = dateArr.concat(timeArr)
  return returnArr
}


//计算两个时间之间的时间差 多少天时分秒
export function intervalTime(startTime, endTime) {
  // var timestamp=new Date().getTime(); //计算当前时间戳
  var timestamp = (Date.parse(new Date())) / 1000;//计算当前时间戳 (毫秒级)
  let date1 = (Date.parse(new Date(startTime))) / 1000;//计算当前时间戳 (毫秒级)
  // var date1 = ""; //开始时间
  // if (timestamp < startTime) {
  //   date1 = startTime;
  // } else {
  //   date1 = timestamp; //开始时间
  // }
  // var date2 = endTime; //结束时间
  var date2 = timestamp; //结束时间
  // var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
  var date3 = (date2 - date1) * 1000; //时间差的毫秒数
  //计算出相差天数
  var days = Math.floor(date3 / (24 * 3600 * 1000));
  //计算出小时数

  var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));

  //计算相差秒数

  var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);
  // return   days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒"
  return { days, hours, minutes, seconds }
}

export function getLeftTimeBySec(leftTime) {
  let c = leftTime
  let d, h, m, s;
  d = parseInt(c / 60 / 60 / 24), //总秒除以一天的秒 得到相差的天数
    h = parseInt(c / 60 / 60 - 24 * d), //然后取完天数之后的余下的秒数再除以每小时的秒数得到小时
    m = parseInt(c / 60 - 24 * 60 * d - 60 * h), //减去天数和小时数的秒数剩下的秒，再除以每分钟的秒数，得到分钟数
    s = parseInt(c - 24 * 60 * 60 * d - 60 * 60 * h - 60 * m) //得到最后剩下的秒数除以1000 就是秒数，再剩下的毫秒自动忽略即可
  // if (!showDay) {
  // h = h + d * 24
  // }
  if (s < 10) {
    s = '0' + s
  }
  if (m < 10) {
    m = '0' + m
  }
  if (h < 10) {
    h = '0' + h
  }
  if (d < 10) {
    d = '0' + d
  }
  if (d > 0) {
    return d + '天:' + h + ':' + m + ':' + s
  }
  return h + ':' + m + ':' + s
}

//获取倒计时格式化时间
export function getLeftTime({ startTime, endTime, showDay = true }) {
  let c = endTime - startTime
  if (c <= 0) {
    return ''
  }
  let d, h, m, s;
  d = parseInt(c / 1000 / 60 / 60 / 24), //总毫秒除以一天的毫秒 得到相差的天数
    h = parseInt(c / 1000 / 60 / 60 - 24 * d), //然后取完天数之后的余下的毫秒数再除以每小时的毫秒数得到小时
    m = parseInt(c / 1000 / 60 - 24 * 60 * d - 60 * h), //减去天数和小时数的毫秒数剩下的毫秒，再除以每分钟的毫秒数，得到分钟数
    s = parseInt(c / 1000 - 24 * 60 * 60 * d - 60 * 60 * h - 60 * m) //得到最后剩下的毫秒数除以1000 就是秒数，再剩下的毫秒自动忽略即可
  if (!showDay) {
    h = h + d * 24
  }
  if (s < 10) {
    s = '0' + s
  }
  if (m < 10) {
    m = '0' + m
  }
  if (h < 10) {
    h = '0' + h
  }
  if (d < 10) {
    d = '0' + d
  }
  if (d > 0) {
    return d + '天' + h + '小时' + m + '分' + s + '秒'
  }
  return h + ':' + m + ':' + s
}

export function formatDate2(date, format = 'YYYY.MM.DD HH:mm') {
  return DayJs(date).format(format)
}

export function formatDate3(date, format = 'YYYY.MM.DD HH:mm:ss') {
  return DayJs(date).format(format)
}

export function formatDate4(date, format = 'YYYY.MM.DD') {
  return DayJs(date).format(format)
}

//格式化预约时间
export function formatScheduledTime(time){
  let date = new Date()
  date.setHours(23)
  date.setMinutes(59)
  date.setSeconds(59)
  let time1Long = new Date(...getDateArr(time))

  if(time1Long.getTime() > date.getTime()){
      return '明天' + formatDate(time,"hh:mm")
  }else{
      return formatDate(time,"hh:mm")
  }
}

